import storage from "@/utils/storage.js";
import config from '@/config/config';
let FromMixin = {
	data() {
		return {
			config,
			minCommission: 2, //最小任务金额
			rulesForm: {
				contactName: {
					rules: [{
						required: true,
						errorMessage: '请填写联系人',
					}],
				},
				contactPhone: {
					rules: [{
						required: true,
						errorMessage: '请填写联系电话',
					}],
				},
				startAddress: {
					rules: [{
						validateFunction: (rule, value, data, callback) => {
							// 只有在特定任务类型时才验证
							if ((data.type === '搬运服务' || data.type === '帮我取' || data.type === '接送小孩') && !value) {
								callback('请填写任务起点')
							}
							return true
						}
					}],
				},
				endAddress: {
					rules: [{
						required: true,
						errorMessage: '请填写任务终点',
					}],
				},
				description: {
					rules: [{
						required: true,
						errorMessage: '请填写任务详情',
					}],
				},
				reward: {
					rules: [{
						required: true,
						errorMessage: '请填写任务赏金',
					}, {
						validateFunction: (rule, value, data, callback) => {
							// 使用箭头函数确保 this 指向组件实例
							const minFee = this.minCommission || 2; // 默认值为2
							const numValue = Number(value)
							// 校验逻辑
							if (isNaN(numValue)) {
								callback('必须为数字')
							} else if (numValue < minFee) {
								callback(`任务赏金不能低于${minFee}元`)
							}
							return true
						}
					}],
				},
				onlyOwner: {
					rules: [{
						required: true,
						errorMessage: '请选择是否仅限业主接单',
					}],
				},
				serviceTime: {
					rules: [{
						required: true,
						errorMessage: '请选择服务时间',
					}],
				},
				itemType: {
					rules: [{
						validateFunction: (rule, value, data, callback) => {
							// 只有在任务类型为"搬运服务"或"帮我取"时才验证
							if ((data.type === '搬运服务' || data.type === '帮我取') && !value) {
								callback('请选择物品类别')
							}
							return true
						}
					}],
				},
				expiresAt: {
					rules: [{
						required: true,
						errorMessage: '请选择自动取消时间',
					}],
				},
				pickupCode: {
					rules: [{
						validateFunction: (rule, value, data, callback) => {
							// 只有在任务类型为"帮我取"时才验证
							if (data.type === '帮我取' && !value) {
								callback('请填写取件码')
							}
							return true
						}
					}],
				},
			},
			rangeType: [{
					value: '万能帮办',
					text: '万能帮办',
				}, {
					value: '搬运服务',
					text: '搬运服务',
				},
				{
					value: '照顾宠物',
					text: '照顾宠物',
				},

				{
					value: '帮我买',
					text: '帮我买',
				},

				{
					value: '帮我取',
					text: '帮我取',
				},
				{
					value: '家庭维修',
					text: '家庭维修',
				},
				{
					value: '接送小孩',
					text: '接送小孩',
				},

			], //类型下拉框
			// 单选数据源
			categoryType: [{
				text: '家具',
				value: 0
			}, {
				text: '小件物品',
				value: 1
			}, {
				text: '大件包裹',
				value: 2
			}, {
				text: '其他',
				value: 3
			}],
			//自动取消订单时间
			autoCancelTimeTab: [{
					value: '一小时后',
					text: '一小时后',
				},
				{
					value: '两小时后',
					text: '两小时后',
				},
				{
					value: '三小时后',
					text: '三小时后',
				}
			],
			//是否仅限业主接单
			list: [{
				text: '是',
				value: 1
			}, {
				text: '否',

				value: 0
			}],
			imgStyle: {

				"border": { // 如果为 Boolean 值，可以控制边框显示与否
					"color": "#ccc", // 边框颜色
					"radius": "8" // 边框圆角，支持百分比
				}
			},
		}
	},
	onLoad() {

	},
	onReady() {
		this.$refs.form.setRules(this.rulesForm)
	},
	methods: {
		uploadImages(e) {
			let self = this
			this.tempFilePath = e.tempFilePaths[0]
			if (this.tempFilePath) {
				const token = storage.getAccessToken()
				uni.uploadFile({
					url: `${config.commonApi}/api/upload/wechat-image`,
					filePath: this.tempFilePath,
					name: 'file',
					header: {
						'authorization': token
					},
					success(uploadFileRes) {
						let data = JSON.parse(uploadFileRes.data)
						let imageData = {
							url: data.data.url
						}
						self.formData.images.push(imageData)
					}
				})
			}
		},
	}
}

export default FromMixin;